package com.example.educational.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.educational.controller.utils.Code;
import com.example.educational.controller.utils.Result;
import com.example.educational.entity.NoticeReceived;
import com.example.educational.entity.User;
import com.example.educational.service.CorporateService;
import com.example.educational.service.NoticeReceivedService;
import com.example.educational.service.SendMailService;
import com.example.educational.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * 作用：接收爬虫-获取公告留言的消息内容
 * 描述：
 * 技术难点：
 * 踩坑日志：1.获取推送通知信息时需要进行数据校验，因为把程序部署的时候，程序经常接收到外部服务的垃圾请求，而无故报错
 * 2.
 */
@Slf4j
@RestController
@RequestMapping("/message")
public class EducationalController {


    @Autowired
    UserService userService;

    @Autowired
    NoticeReceivedService noticeReceivedService;

    @Autowired
    CorporateService corporateService;

    @Autowired
    SendMailService sendMailService;


    /**
     * 业务执行逻辑：1.完成数据校验，保证数据的完整性。2.完成数据的存储，确保python脚本获取的通知消息成功保存
     *
     * @param jsonStrObj 脚本获取到的通知消息所传输的Json格式文本
     * @return 消息通知存储代码信息
     */
    @ApiOperation(value = "推送通知信息")
    @PostMapping("/notice")
    private Result receiveNoticeData(@RequestBody String jsonStrObj) {
        // 1.数据校验
        NoticeReceived noticeReceived = noticeReceivedService.checkReceiveNotice(jsonStrObj);
        log.info("数据校验完成~");
        // 2.数据存储
        Boolean resultStorage = noticeReceivedService.savaNoticeReceived(noticeReceived);
        log.info("通知信息已存储~");
        if (resultStorage) {
            // 3.数据推送
            Boolean resultPush = corporateService.pushNotice2Everyone(noticeReceived);
            log.info("消息推送成功~");
            return new Result(Code.success, null, "数据接收完成~");
        } else {
            return new Result(Code.success_again, null, "该消息已通知，无需重复获取~");
        }
    }

    /**
     * @param qq
     * @param studyNum
     * @param phone
     * @param organization
     * @return
     */
    @ApiOperation(value = "内部接口，禁止调用")
    @GetMapping("first")
    private Result getAllUser(@RequestParam String qq, @RequestParam String studyNum, @RequestParam String phone, @RequestParam String organization
    ) {
        // 1.验证QQ
        if (ObjectUtil.notEqual(qq, "1411850978") || ObjectUtil.notEqual(studyNum, "202112094263") || ObjectUtil.notEqual(phone, "15984882170") || ObjectUtil.notEqual(organization, "DreamStudio")) {
            return new Result(Code.forbid_bind, null, "内部接口，禁止调用!!!");
        }
        // 2.获取人员列表
        ArrayList<User> informationFromEveryone = corporateService.getUserList();
        // 3.插入数据
        boolean result;
        result = userService.saveBatch(informationFromEveryone);
        log.info("获取所有人消息成功~~");
        return new Result(Code.success, result, "人员插入完成~");
    }

    @ApiOperation(value = "部署邮件测试")
    @GetMapping("/email")
    private Result getEmailState() {
        sendMailService.sendMail("教务管理", "邮件接通成功~");
        log.info("邮件通知成功~");
        return new Result("200", "邮件已推送~");
    }

    @ApiOperation(value = "测试网络状态")
    @GetMapping
    private Result getNetWork() {
        log.info("网络测试成功~");
        return new Result("200", null, "网络通畅");
    }
}
